package com.yxhl.km.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxhl.km.R;
import com.yxhl.km.module.entity.EventBean;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 张亚楠 on 2017/10/30.
 */

public abstract class BaseFragment extends android.support.v4.app.Fragment implements View.OnClickListener {
    protected Context mContext;
    protected View view;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int layoutId = getLayoutId();
        if (layoutId != 0) {
            view = inflater.inflate(layoutId, null);
            unbinder = ButterKnife.bind(this, view);
        }
        mContext = this.getActivity();
        preliminary();
        if (!EventBus.getDefault().isRegistered(this)) {
            //注册事件
            EventBus.getDefault().register(this);
        }
        return view;
    }

    /**
     * 标题
     */
    protected TextView titlebar_left_text, titlebar_title, titlebar_right_text;
    protected ImageView titlebar_right_iv;

    /**
     * 向用户展示信息前的准备工作在这个方法里处理
     */
    protected void preliminary() {

        // 初始化数据
        initialized();

        // 初始化组件
        initListener();
    }

    /**
     * 布局文件ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化监听
     */
    protected abstract void initListener();

    /**
     * 初始化数据
     */
    protected abstract void initialized();

    protected void initTitle(String left, String title, String right) {
        titlebar_title = (TextView) view.findViewById(R.id.titlebar_title_text);
        titlebar_left_text = (TextView) view.findViewById(R.id.titlebar_left_text);
        titlebar_right_text = (TextView) view.findViewById(R.id.titlebar_right_text);
        titlebar_right_iv = (ImageView) view.findViewById(R.id.titlebar_right_iv);


        if (left == null) {
            titlebar_left_text.setVisibility(View.INVISIBLE);
        } else {
            titlebar_left_text.setText(left);
            titlebar_left_text.setVisibility(View.VISIBLE);
        }
        if (right == null) {
            titlebar_right_text.setVisibility(View.INVISIBLE);
        } else {
            titlebar_right_text.setText(right);
            titlebar_right_text.setVisibility(View.VISIBLE);
        }

        if (title == null) {
            titlebar_right_text.setVisibility(View.INVISIBLE);
        } else {
            titlebar_title.setText(title);
            titlebar_right_text.setVisibility(View.VISIBLE);
        }
        titlebar_left_text.setOnClickListener(this);
        titlebar_right_text.setOnClickListener(this);
        titlebar_right_iv.setOnClickListener(this);
        titlebar_title.setOnClickListener(this);
    }

    /**
     * 动画跳转Activity
     *
     * @param clazz
     * @param finish
     * @param bundle
     */
    public void gotoActivity(Class<? extends Activity> clazz, boolean finish, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle != null) {
            intent.putExtra("Bundle", bundle);
        }
        this.startActivity(intent);
        if (finish) {
            getActivity().finish();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            //设置切换动画，从右边进入，左边退出
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }


    public UserInfoBean getUserInfo() {
        UserInfoBean userInfoBean = (UserInfoBean) SharedPreferencesUtils.getObjectFromShare(mContext, Constants.USERINFO);
        if (userInfoBean == null) {
            return null;

        }
        return userInfoBean;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(EventBean eventBean) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }
}
